home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / toolbar / toolbar.bak < prev    next >
Text File  |  1992-07-01  |  10KB  |  395 lines

  1. /*
  2.  *  Routines for the TOOLBAR library
  3.  *  Copyright (C) Stephen Chung, 1991.
  4.  *  All rights reserved.
  5.  *
  6.  *  Portions (C) Tim Liddelow 1992
  7.  */
  8.  
  9. #include <windows.h>
  10. #include "toolbar.h"
  11.  
  12. #define MAXSTATES       3
  13.  
  14. BOOL ToolBar::ClassRegistered = FALSE;
  15. BOOL ToolBar::Capturing = FALSE;
  16.  
  17. void ToolBar::Resize (int x, int y, int width, int height, BOOL bRepaint)
  18. {
  19.     MoveWindow (hwnd, x, y, width, height, bRepaint);
  20. }
  21.  
  22. void ToolBar::EnableToolbarButton (int id, BOOL enabled)
  23. {
  24.     int             i, n;
  25.     TOOLBARICON     *icons;
  26.  
  27.     icons = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  28.     n = (int) GetWindowWord(hwnd, sizeof(WORD));
  29.  
  30.     for (i = 0; i < n && icons[i].id != id; i++);
  31.  
  32.     if (i >= n) return;
  33.  
  34.     if (enabled)
  35.     {
  36.        if (icons[i].state >= 0) return;
  37.        icons[i].state = icons[i].oldstate;
  38.     }
  39.     else
  40.     {
  41.        if (icons[i].state < 0) return;
  42.        icons[i].oldstate = icons[i].state;
  43.        icons[i].state = -1;
  44.     }
  45.     InvalidateRect(icons[i].hwnd, NULL, FALSE);
  46.     UpdateWindow(icons[i].hwnd);
  47. }
  48.  
  49. HWND ToolBar::GetToolbarButton (int id, TOOLBARICON *ip)
  50. {
  51.     int             i, n;
  52.     TOOLBARICON     *icons;
  53.  
  54.     icons = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  55.     n = (int) GetWindowWord(hwnd, sizeof(WORD));
  56.  
  57.     for (i = 0; i < n && icons[i].id != id; i++);
  58.  
  59.     if (i >= n) return (NULL);
  60.  
  61.     if (ip != NULL) *ip = icons[i];
  62.  
  63.     return (icons[i].hwnd);
  64. }
  65.  
  66. void ToolBar::ModifyToolbarButton (TOOLBARICON *icon)
  67. {
  68.     TOOLBARICON *old;
  69.  
  70.     old = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  71.  
  72.     old->id = icon->id;
  73.     old->x = icon->x;
  74.     old->y = icon->y;
  75.     old->width = icon->width;
  76.     old->height = icon->height;
  77.     old->state = icon->state;
  78.     old->cycle = icon->cycle;
  79.     old->disabled = icon->disabled;
  80.     old->undepressed = icon->undepressed;
  81.     old->depressed = icon->depressed;
  82.     old->grayed = icon->grayed;
  83.     old->pressing = icon->pressing;
  84.  
  85.     InvalidateRect(hwnd, NULL, TRUE);
  86.     UpdateWindow(hwnd);
  87. }
  88.  
  89. void ToolBar::ToolBar (HWND parent, int x, int y, int width, int height,
  90.                     int thickness, int id, int n, HANDLE hInstance,
  91.                     TOOLBARICON *icons, char *xcursor)
  92. {
  93.     int         i;
  94.     WNDCLASS    wndclass;
  95.  
  96.     if (!ClassRegistered)
  97.     {
  98.        wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  99.        wndclass.lpfnWndProc    = ToolbarProc;
  100.        wndclass.cbClsExtra     = 0;
  101.        wndclass.cbWndExtra     = 4 * sizeof(WORD);
  102.        wndclass.hInstance      = hInstance;
  103.        wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  104.        wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  105.        wndclass.hbrBackground  = COLOR_BTNFACE + 1;
  106.        wndclass.lpszMenuName   = NULL;
  107.        wndclass.lpszClassName  = TOOLBARCLASS;
  108.  
  109.        RegisterClass(&wndclass);
  110.  
  111.        wndclass.cbWndExtra     = sizeof(WORD);
  112.        wndclass.lpfnWndProc    = ToolbarButtonProc;
  113.        wndclass.lpszClassName  = TOOLBARBUTTONCLASS;
  114.  
  115.        RegisterClass(&wndclass);
  116.     }
  117.  
  118.     hwnd = CreateWindow(TOOLBARCLASS, "", WS_CHILD | WS_VISIBLE,
  119.                         x, y, width, height, parent, id, hInstance, NULL);
  120.  
  121.     SetWindowWord(hwnd, 0, (WORD) icons);
  122.     SetWindowWord(hwnd, sizeof(WORD), (WORD) n);
  123.     SetWindowWord(hwnd, 2 * sizeof(WORD), (WORD) thickness);
  124.     if (xcursor != NULL)
  125.        SetWindowWord(hwnd, 3 * sizeof(WORD), LoadCursor(hInstance, xcursor));
  126.     else
  127.        SetWindowWord(hwnd, 3 * sizeof(WORD), NULL);
  128.     ShowWindow(hwnd, SW_SHOW);
  129.     UpdateWindow(hwnd);
  130.  
  131.     /* Create the children */
  132.  
  133.     for (i = 0; i < n; i++)
  134.     {
  135.       icons[i].hwnd = CreateWindow(TOOLBARBUTTONCLASS, "",
  136.                                  WS_CHILD | WS_VISIBLE,
  137.                                  icons[i].x, icons[i].y,
  138.                                  icons[i].width, icons[i].height,
  139.                                  hwnd, icons[i].id, hInstance, NULL);
  140.  
  141.       SetWindowWord(icons[i].hwnd, 0, (WORD) &(icons[i]));
  142.       ShowWindow(icons[i].hwnd, SW_SHOW);
  143.       UpdateWindow(hwnd);
  144.     }
  145. }
  146.  
  147. long FAR PASCAL ToolBar::ToolbarProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  148. {
  149.     int             i;
  150.     HDC             hdc;
  151.     PAINTSTRUCT     ps;
  152.     HWND            child;
  153.     RECT            rect;
  154.     TOOLBARICON     *icons;
  155.  
  156.  
  157.     switch (message)
  158.     {
  159.         case WM_PAINT:
  160.             GetClientRect(hwnd, &rect);
  161.             hdc = BeginPaint(hwnd, &ps);
  162.             Create3DEffect(hdc, &rect, GetWindowWord(hwnd, 2 * sizeof(WORD)), 0);
  163.             EndPaint(hwnd, &ps);
  164.             return (0);
  165.  
  166.         case WM_COMMAND:
  167.             SendMessage(GetParent(hwnd), WM_COMMAND,
  168.                         (wParam << 8) | GetWindowWord(hwnd, GWW_ID), lParam);
  169.             return (0);
  170.  
  171.         case BM_SETSTATE:
  172.         case BM_GETSTATE:
  173.             icons = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  174.             for (i = 0; icons[i].id != LOWORD(lParam); i++);
  175.             return (SendMessage(icons[i].hwnd, message, wParam, 0L));
  176.     }
  177.     return (DefWindowProc(hwnd, message, wParam, lParam));
  178. }
  179.  
  180. long FAR PASCAL ToolBar::ToolbarButtonProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  181. {
  182.     int             i, j;
  183.     BOOL            crossed;
  184.     HDC             hdc, hdcmem;
  185.     PAINTSTRUCT     ps;
  186.     HPEN            hpen;
  187.     HWND            child;
  188.     TOOLBARICON     *icon;
  189.     HBITMAP         hbitmap;
  190.     HANDLE            hInstance;
  191.     HCURSOR         hcursor;
  192.  
  193.  
  194.     icon = (TOOLBARICON *) GetWindowWord(hwnd, 0);
  195.  
  196.     if (icon->state < 0)
  197.     {
  198.        hcursor = (HCURSOR) GetWindowWord(GetParent(hwnd), 3 * sizeof(WORD));
  199.        if (hcursor != NULL)
  200.           SetCursor(hcursor);
  201.     }
  202.  
  203.     switch (message)
  204.     {
  205.  
  206.       case BM_SETSTATE:
  207.         if (icon->state == wParam)
  208.            return (0);
  209.  
  210.         icon->oldstate = icon->state = wParam;
  211.         InvalidateRect(hwnd, NULL, FALSE);
  212.         UpdateWindow(hwnd);
  213.         return (0);
  214.  
  215.       case BM_GETSTATE:
  216.         return (icon->state);
  217.  
  218.       case WM_LBUTTONDOWN:
  219.         if (icon->state < 0) return (0);
  220.  
  221.         icon->oldstate = icon->state;
  222.         icon->state = MAXSTATES;
  223.         InvalidateRect(hwnd, NULL, FALSE);
  224.         UpdateWindow(hwnd);
  225.         SetCapture(hwnd);
  226.         ToolBar::Capturing = TRUE;
  227.         return (0);
  228.  
  229.       case WM_LBUTTONUP:
  230.         if (!ToolBar::Capturing || icon->state < 0) return (0);
  231.  
  232.         ReleaseCapture();
  233.         ToolBar::Capturing = FALSE;
  234.  
  235.         i = LOWORD(lParam);
  236.         j = HIWORD(lParam);
  237.  
  238.         if (i < 0 || i >= icon->width || j < 0 || j >= icon->height)
  239.         {
  240.            return (0);
  241.         }
  242.         else
  243.         if (icon->cycle <= 0)
  244.         {
  245.            icon->state = icon->oldstate;
  246.         }
  247.         else
  248.         {
  249.            icon->oldstate++;
  250.            if (icon->oldstate >= icon->cycle) icon->oldstate = 0;
  251.            icon->state = icon->oldstate;
  252.         }
  253.  
  254.         InvalidateRect(hwnd, NULL, FALSE);
  255.         UpdateWindow(hwnd);
  256.         SendMessage(GetParent(hwnd), WM_COMMAND, icon->id, MAKELONG(hwnd, BN_CLICKED));
  257.         return (0);
  258.  
  259.       case WM_MOUSEMOVE:
  260.         if (!Capturing || icon->state < 0) return (0);
  261.  
  262.         i = LOWORD(lParam);
  263.         j = HIWORD(lParam);
  264.  
  265.         if (i < 0 || i >= icon->width || j < 0 || j >= icon->height)
  266.         {
  267.             crossed = (icon->state == MAXSTATES);
  268.             icon->state = icon->oldstate;
  269.         }
  270.         else
  271.         {
  272.            crossed = (icon->state != MAXSTATES);
  273.            icon->state = MAXSTATES;
  274.         }
  275.  
  276.         if (crossed)
  277.         {
  278.            InvalidateRect(hwnd, NULL, FALSE);
  279.            UpdateWindow(hwnd);
  280.         }
  281.         return (0);
  282.  
  283.     case WM_PAINT:
  284.         hInstance = GetWindowWord(hwnd, GWW_HINSTANCE);
  285.  
  286.         hdc = BeginPaint(hwnd, &ps);
  287.  
  288.         switch (icon->state)
  289.         {
  290.           default:
  291.             if (icon->disabled != NULL)
  292.             {
  293.                hbitmap = LoadBitmap(hInstance, icon->disabled);
  294.             }
  295.             break;
  296.  
  297.           case 0:  hbitmap = LoadBitmap(hInstance, icon->undepressed); break;
  298.           case 1:  hbitmap = LoadBitmap(hInstance, icon->depressed); break;
  299.           case 2:  hbitmap = LoadBitmap(hInstance, icon->grayed); break;
  300.  
  301.           case MAXSTATES:
  302.             hbitmap = LoadBitmap(hInstance, icon->pressing); break;
  303.         }
  304.  
  305.         hdcmem = CreateCompatibleDC(hdc);
  306.         SelectObject(hdcmem, hbitmap);
  307.  
  308.         BitBlt(hdc, 0, 0, icon->width, icon->height, hdcmem, 0, 0, SRCCOPY);
  309.  
  310.         DeleteDC(hdcmem);
  311.         DeleteObject(hbitmap);
  312.         EndPaint(hwnd, &ps);
  313.         return (0);
  314.  
  315.     }
  316.     return (DefWindowProc(hwnd, message, wParam, lParam));
  317. }
  318.  
  319. void Create3DEffect (HDC hdc, RECT *rect, int thickness, int style)
  320. {
  321.     int     i;
  322.     int     x1 = rect->left;
  323.     int     y1 = rect->top;
  324.     int     x2 = rect->right;
  325.     int     y2 = rect->bottom;
  326.     HBRUSH  hbrush, oldbrush;
  327.     HPEN    hpen, oldpen;
  328.  
  329.     oldpen = SelectObject(hdc, GetStockObject(BLACK_PEN));
  330.     switch (style)
  331.     {
  332.       default:
  333.       case 0:
  334.       case 1:    hbrush = GetStockObject(LTGRAY_BRUSH); break;
  335.       case 2:    hbrush = GetStockObject(WHITE_BRUSH); break;
  336.     }
  337.  
  338.     oldbrush = SelectObject(hdc, hbrush);
  339.     Rectangle(hdc, x1, y1, x2, y2);
  340.  
  341.     switch (style)
  342.     {
  343.       default:
  344.       case 0:    SelectObject(hdc, GetStockObject(WHITE_PEN)); break;
  345.       case 1:    hpen = CreatePen(PS_SOLID, 1, RGB(128,128,128));
  346.                 SelectObject(hdc, hpen);
  347.                 break;
  348.       case 2:    SelectObject(hdc, GetStockObject(BLACK_PEN)); break;
  349.     }
  350.  
  351.     for (i = 1; i <= thickness; i++)
  352.     {
  353.       MoveTo(hdc, x1 + i, y1 + i); LineTo(hdc, x1 + i, y2 - 1);
  354.       MoveTo(hdc, x1 + i, y1 + i); LineTo(hdc, x2 - 1, y1 + i);
  355.     }
  356.  
  357.     switch (style)
  358.     {
  359.       default:
  360.       case 2:
  361.       case 0:    hpen = CreatePen(PS_SOLID, 1, RGB(128,128,128));
  362.                 SelectObject(hdc, hpen);
  363.                 break;
  364.       case 1:    SelectObject(hdc, GetStockObject(WHITE_PEN)); break;
  365.     }
  366.  
  367.     for (i = 1; i <= thickness; i++)
  368.     {
  369.       MoveTo(hdc, x1 + i, y2 - 1 - i);
  370.       LineTo(hdc, x2 - 1, y2 - 1 - i);
  371.       MoveTo(hdc, x2 - 1 - i, y2 - 2);
  372.       LineTo(hdc, x2 - 1 - i, y1 + i);
  373.     }
  374.  
  375.     SelectObject(hdc, oldbrush);
  376.     SelectObject(hdc, oldpen);
  377.     DeleteObject(hpen);
  378. }
  379.  
  380. static void CursorShape (HWND hwnd, int state)
  381. {
  382.     HCURSOR hCursor;
  383.  
  384.     static    Arrow = NULL;
  385.  
  386.     if (state < 0)
  387.        hCursor = (HCURSOR) GetWindowWord(GetParent(hwnd), 3 * sizeof(WORD));
  388.     else
  389.     if (Arrow == NULL)
  390.        hCursor = Arrow = LoadCursor(NULL, IDC_ARROW);
  391.     else
  392.        hCursor = Arrow;
  393.     SetCursor(hCursor);
  394. }
  395.